<?php
/*
 * @Author: your name
 * @Date: 2020-11-24 10:44:57
 * @LastEditTime: 2020-12-31 17:27:09
 * @LastEditors: Please set LastEditors
 * @Description: windows2008规则串解析为数组
 * @FilePath: /special/application/adminapiv3/service/module_manage/netfilter/rule_log_service/RuleLogWindows2008RuleService.php
 */
namespace app\adminapiv3\service\module_manage\netfilter\rule_log_service;

use app\common\definition\InternetProtocolsDefinition;
use app\common\exception\CustomException;

class RuleLogWindows2008RuleService extends RuleLogRuleService
{
    /**
     * @name: 设置处理方式
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-23 13:01:31
     */
    public function setProcess ()
    {
        preg_match('/action\=(\w+)/i', $this->content, $math);
        if (!empty($math))
        {
            if ($math[1] == 'allow')
            {
                $return = self::PROCESS_ALLOW;
            }
            else
            {
                $return = self::PROCESS_DROP;
            }
        }
        else
        {
           throw new CustomException('未找到处理方式', 422);
        }

        return $return;
    }

    /**
     * @name: 设置方向
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-23 12:56:55
     */
    public function setDirection ()
    {
        preg_match('/dir\=(\w+)/i', $this->content, $math);
        if (empty($math))
        {
           throw new CustomException('未找到流量方向', 422);
        }

        if (strtolower($math[1]) == 'output')
        {
            $return = self::DIRECTION_OUTPUT;
        }
        else
        {
            $return = self::DIRECTION_INPUT;
        }
        
        return $return;
    }

    /**
     * @name: 设置源ip
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-23 13:04:28
     */
    public function setLocalIp ()
    {
        preg_match('/localip\=(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(-\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?)/i', $this->content, $math);

        return $this->processIp($math);
        
        
    }

    /**
     * @name: 目标ip
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-23 12:57:40
     */
    public function setRemoteIp()
    {
        preg_match('/remoteip\=(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(-\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?)/i', $this->content, $math);

        return $this->processIp($math);
    }

    /**
     * @name: 源端口
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-23 12:58:03
     */
    public function setLocalPort ()
    {
        preg_match('/localport=(\d+([,-]\d+){0,13})/i', $this->content, $math);

        return $this->processPort($math, '-');
    }

    /**
     * @name: 目标端口
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-23 12:58:03
     */
    public function setRemotePort ()
    {
        preg_match('/remoteport=(\d+([,-]\d+){0,13})/i', $this->content, $math);

        return $this->processPort($math, '-');
    }

    /**
     * @name: 协议
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-23 12:58:03
     */
    public function setProtocols ()
    {
        preg_match('/protocol\=(\w+)/i', $this->content, $math);

        if (empty($math))
        {
            return InternetProtocolsDefinition::ALL_PROTOCOL;
        }

        switch (strtolower($math[1]))
        {
            case 'tcp' :
                $return = InternetProtocolsDefinition::TCP_PROTOCOL;
            break;
            case 'udp' :
                $return = InternetProtocolsDefinition::UDP_PROTOCOL;
            break;
            default :
                $return = InternetProtocolsDefinition::ALL_PROTOCOL;
        }

        return $return;
    }

    /**
     * @name: 设置规则应用域
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-12-04 14:45:12
     */
    public function setScope ()
    {
        preg_match('/profile\=((\w+)(,\w+){0,2})/i', $this->content, $math);

        if (empty($math))
        {
            return '';
        }

        return $math[1];
    }

    /**
     * @name: 设定数据
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-24 13:15:22
     */
    public function setData ()
    {
        $this->direction = $this->setDirection();
        $local_ip = $this->setLocalIp();
        $local_port = $this->setLocalPort();
        $remote_ip = $this->setRemoteIp();
        $remote_port = $this->setRemotePort();
        if ($this->direction == 1)
        {
            $this->src_port = $local_port;
            $this->src_ip = $local_ip;
            $this->dest_port = $remote_port;
            $this->dest_ip = $remote_ip;
        }
        else
        {
            $this->src_port = $remote_port;
            $this->src_ip = $remote_ip;
            $this->dest_port = $local_port;
            $this->dest_ip = $local_ip;
        }
        
        $this->process = $this->setProcess();
        $this->protocols = $this->setProtocols();
        $this->scope = $this->setScope();
        
    }
}